﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using Sell.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sell.Services.Suppliers
{
    public class SupplierServices : ISupplierServices
    {
        public ISupplierRepository Supplierss { get;}
        public SupplierServices(ISupplierRepository _Supplierss)
        {
            Supplierss = _Supplierss;
        }
        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddSupplier(Supplier supplier)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "供应商管理添加成功";
            var list = await Supplierss.AddAsync(supplier);
            res.Data = list;
           return await Task.FromResult(res);
        }

        public async Task<ApiResult<int>> DeleteSupplier(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                //根据ID获取客户信息
                var supp = await Supplierss.GetModelAsync(id);
                if (supp == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "供应商不存在";
                    return res;
                }
                //将IsDelete状态设置为true
                supp.IsDelete = true;
                //更新客户信息
                var result = await Supplierss.Update(supp);
                res.Code = EnumCode.Ok;
                res.Message = "供应商删除成功";
                res.Data = result;

            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"删除供应商失败:{ex.Message}";
            }
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 供应商管理修改
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateSupplier(Supplier supplier)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "供应商管理修改成功";
            var list = await Supplierss.Update(supplier);
            res.Data = list;
            return await Task.FromResult(res);
        }
    }
}
